package cn.edu.neosoft.sunbo509.finefood.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.LoginBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class LoginActivity extends AppCompatActivity implements View. OnClickListener{
    private EditText et_person, et_loginpwd;
    private Button btn_register, btn_login;
    private CheckBox cb_ispwd;
    private SharedPreferences sp;
    private String userid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        InitView();

        Intent intent = getIntent();
        String recymessage = intent.getStringExtra("message");
        et_person.setText(recymessage);

       sp= getSharedPreferences("config",0);
        if (sp.getBoolean("ispwd",false)) {
            String sp_name = sp.getString("name", "");
            String sp_pwd = sp.getString("password", "");
            et_person.setText(sp_name);
            et_loginpwd.setText(sp_pwd);
            cb_ispwd.setChecked(true);
        }
    }
    public void InitView() {
        et_person = (EditText) findViewById(R.id.login_name);
        et_loginpwd = (EditText) findViewById(R.id.login_pwd);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_register = (Button) findViewById(R.id.btn_register);
        cb_ispwd=(CheckBox)findViewById(R.id.is_pwd);
        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        cb_ispwd.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String name=et_person.getText().toString().trim();
                String pwd =et_loginpwd.getText().toString().trim();
                if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
                    Toast.makeText(LoginActivity.this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else {
                    if (cb_ispwd.isChecked()) {
                        sp = getSharedPreferences("config", 0);
                       SharedPreferences.Editor edit = sp.edit();
                        edit.putBoolean("ispwd", true);
                        edit.commit();
                        GetDate();
                    } else {
                        sp= getSharedPreferences("config",0);
                        SharedPreferences.Editor edit=sp.edit();
                        edit.putBoolean("ispwd",false);
                        edit.commit();
                        GetDate();
                    }
                }
                break;
            case R.id.btn_register:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                Toast.makeText(LoginActivity.this, "跳转到注册页面", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
  public void GetDate(){
      final String person=et_person.getText().toString();
      final String pwd=et_loginpwd.getText().toString();
      Retrofit retrofit=new Retrofit.Builder()
              .baseUrl(BaseUrl.BASEURL)
              .addConverterFactory(GsonConverterFactory.create())
              .build();
      Service service=retrofit.create(Service.class);
      Call<LoginBean>call=service.getLogin(person,pwd);
      call.enqueue(new Callback<LoginBean>() {
          @Override
          public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
               userid =response.body().getUserid();
              if(!userid.equals("0")) {
                  sp = getSharedPreferences("config", 0);
                  SharedPreferences.Editor edit = sp.edit();
                  edit.putString("name", person);
                  edit.putString("password", pwd);
                  edit.commit();
                  Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                  intent.putExtra("userid",userid);
                  startActivity(intent);
                  finish();
                  Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
              }
              else{
                  Toast.makeText(LoginActivity.this, "用户名和密码不正确", Toast.LENGTH_SHORT).show();
              }
          }

          @Override
          public void onFailure(Call<LoginBean> call, Throwable t) {
              Log.e("error", t.getMessage());
          }
      });
  }


}
